package lx.af.demo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import lx.af.R;
import lx.af.activity.ActivityCropImage;
import lx.af.base.BaseActivity;
import lx.af.utils.log.Log;

/**
 * Created by liuxu on 15-2-9.
 */
public class ActivityAvatarDemo extends BaseActivity
        implements View.OnClickListener, BaseActivity.ActionBarImpl {

    // activity request and result code
    private static final int AC_RESULT_CODE_NONE = 0;
    private static final int AC_REQUEST_CODE_CAMERA = 1; // camera
    private static final int AC_REQUEST_CODE_GALLERY = 2; // crop image
    private static final int AC_REQUEST_CODE_CROP = 3; // final result

    private ImageView mAvatar;
    private String mImgPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_avatar);
        findViewById(R.id.activity_avatar_btn_from_camera).setOnClickListener(this);
        findViewById(R.id.activity_avatar_btn_from_gallery).setOnClickListener(this);
        mAvatar = getView(R.id.activity_avatar_img);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "onActivityResult, requestCode="
                + requestCode + ", resultCode=" + resultCode);
        if (resultCode == AC_RESULT_CODE_NONE) {
            return;
        }

        switch (requestCode) {
            case AC_REQUEST_CODE_CAMERA: {
                File picture = new File(mImgPath);
                startCrop(Uri.fromFile(picture), AC_REQUEST_CODE_CROP);
                break;
            }
            case AC_REQUEST_CODE_GALLERY: {
                if (data == null) {
                    return;
                }
                startCrop(data.getData(), AC_REQUEST_CODE_CROP);
                break;
            }
            case AC_REQUEST_CODE_CROP: {
                if (data == null) {
                    return;
                }
                Uri uri = data.getData();
                Log.d(TAG, "onActivityResult, crop pic uri: " + uri);
                if (uri != null) {
                    String path = uri.getPath();
                    Bitmap avatar = BitmapFactory.decodeFile(path);
                    mAvatar.setImageBitmap(avatar);
                }

                break;
            }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_avatar_btn_from_camera: {
                mImgPath = getImagePath();
                File file = new File(mImgPath);
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(intent, AC_REQUEST_CODE_CAMERA);
                break;
            }
            case R.id.activity_avatar_btn_from_gallery: {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, AC_REQUEST_CODE_GALLERY);
                break;
            }
        }
    }

    // generate file name with current time
    private static String getImagePath() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String imgPath = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM).toString() +
                "/lxdemo_" + df.format(new Date()) + ".jpg";
        return imgPath;
    }

    public void startCrop(Uri uri, int requestCode) {
        new ActivityCropImage.CropBuilder(uri, requestCode)
                .output(new File(getImagePath()))
                .withWidth(200)
                .withQuality(75)
                .start(this);
    }
}
